package com.dao;

import java.sql.*;

public class PersonalJdbc {
    Connection conn;
    Statement stem;
    ResultSet rs;
    public void lianjie(){
        try{
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://120.76.45.216:3306/buy_ticket","root","root");
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    public ResultSet show(){
        this.lianjie();
        try{
            stem = conn.createStatement();
            rs = stem.executeQuery("select  * from personal");
        }catch (SQLException e){
            e.printStackTrace();
        }
        return rs;
    }
    /**
     * 根据ID查询
     * @param id
     * @return
     */
    public ResultSet selectById(int id){
        this.lianjie();
        try{
            stem=conn.createStatement();
            rs=stem.executeQuery("select * from personal where mid = '"+id+"'");
        }catch (SQLException e){
            e.printStackTrace();
        }
        return rs;
    }

    public void close(){
        try{
            if (conn!=null){
                conn.close();
            }
            if (stem!=null){
                stem.close();
            }
            if (rs!=null){
                rs.close();
            }
        }catch (SQLException e){
            e.printStackTrace();
        }
    }

    public void update(String mid, String realname, String sex, String city, String number, String telephone, String email) {
        this.lianjie();
        try {
            stem=conn.createStatement();
            stem.executeUpdate("update personal set realname='"+realname+"'  , sex='"+sex+"'  , city='"+city+"' ,number='"+number+"' ,telephone='"+telephone+"',email='"+email+"'where mid='"+mid+"'");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        this.close();
    }
}
